﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoanCalculator.Core.Enums;

namespace LoanCalculator.Core.Classes
{
    [Serializable]
    public class Loan : LCBaseEntity
    {
        public double LoanAmount { get; set; }
        public double AnnualInterestRate { get; set; }
        public double Months { get; set; }
        public double DownPayment { get; set; }
        public double PropertyTax { get; set; }
        public double PropertyInsurance { get; set; }// Property Insurance (Home Owner Insurance)

        public Loan()
        {
            LoanAmount = 0.0d;
            AnnualInterestRate = 0.0d;
            Months = 0.0d;
            DownPayment = 0.0d;
            PropertyTax = 0.0d;
            PropertyInsurance = 0.0d;
        }

        public Loan(double loanAmount,
            double annualInterestRate,
            double months,
            double downPayment,
            double propertyTax,
            double propertyInsurance)
            : base()
        {
            LoanAmount = loanAmount;
            AnnualInterestRate = annualInterestRate;
            Months = months;
            DownPayment = downPayment;
            PropertyTax = propertyTax;
            PropertyInsurance = propertyInsurance;
        }

        public double CalculateMonthlyPayment()
        {
            double amount = LoanAmount;
            double months = Months;
            double rate = AnnualInterestRate;
            double monthly = 0.0d;
            double payment = 0.0d;
            double total = 0.0d;
            double interest = 0.0d;
            double downpayment = DownPayment;
            double propertytax = PropertyTax;
            double propertyinsurance = PropertyInsurance;

            rate = (rate / 100);
            monthly = (rate / 12);

            payment =
                (amount - downpayment) *
                (Math.Pow((1 + monthly), months) * rate) /
                (12 * (Math.Pow((1 + monthly), months) - 1));

            total = (payment * months);
            interest = (total - amount);

            return Math.Round(payment += ((propertytax + propertyinsurance)/12), 2);
        }
    }
}
